<?php defined('SYSPATH') or die('No direct script access.');
/**
* Module Dashboard
*/
class Module_users_karyawan {
	
	private $controller;
	private $ss;
	private $message;
	private $thesystems;
	private $mydatetime;
	private $arraylist;
	
	static function &factory() {
	   $o = new Module_users_karyawan();
	  return $o;	
	}
	
	public function __construct()
	{
		$this->controller	= Request::current()->controller();
		$this->ss					= Session::instance();
		$this->thesystems	= Helper_theSystems::factory();
		$this->mydatetime	= Helper_MyDateTime::factory();
		$this->message		= Language_Message::factory();
		$this->arraylist	= Helper_ArrayList::factory();
	}
	
	public function showkaryawan($id, $command)
	{
 		$value_rpp		= Arr::get($_REQUEST,'rpp');
		$rpp			= (!empty($value_rpp) ? $value_rpp : '5');
		$halaman		= Arr::get($_GET,'page',1);
		$offset			= ($halaman - 1)*$rpp;
		
		switch ($command)
		{
		
		case "create":
				  
					  
					// Create Data
					$id            = $_POST['id'];
					$nama          = $_POST['nama'];
					$email         = $_POST['email'];
					$address       = $_POST['address'];
					$phone         = $_POST['phone'];
					$hpassword     = sha1($_POST['hpassword']);
					
				  
				  $query = DB::insert('users', array('id','nama','userroles_key','email','address','phone','hpassword'))
				  ->values(array($id,$nama,200,$email,$address,$phone,$hpassword));
				  $result = $query->execute();
					  if ($result[1] == 1) {
						$this->message->store_message('c_success');
						$r = Request::current();
						$r->redirect($this->controller.'/karyawan');
					  } else {
						$this->message->store_message('c_fails');
					  }
				  break;
		
		case "update":
				  					
					// Update Data
					$p['nama']          = $_POST['nama'];
					$p['id']            = $_POST['id'];
					$p['email']         = $_POST['email'];
					$p['userroles_key'] = 200;
					$p['address']       = $_POST['address'];
					$p['phone']         = $_POST['phone'];
				  
				  $query = DB::update('users')
					  ->set($p)
					  ->where('id', '=', $id)
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
						$this->message->store_message('u_success');
						$r = Request::current();
						$r->redirect($this->controller.'/karyawan?id='.$id);
					  } else {
						$this->message->store_message('u_fails');
					  }
				  break;
		
		case "delete":
				  // Delete Image Files
				  $deletefiles = DB::select()
					  ->from('users')
					  ->where('id', '=', $id)
					  ->execute();
				  $deletefilesresults = $deletefiles->as_array();
				  
				  $dir = 'assets/global/users/';
				  
				  foreach($deletefilesresults as $deletefilesresult)
				  {
					  if (!empty($deletefilesresult['photo']))
					  {
						  unlink('assets/global/users/'.$deletefilesresult['photo']);
					  }
					}
				  
				  // Delete User
				  $query = DB::delete('users')
					  ->where('id', '=', $id)
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
						$this->message->store_message('d_success');
						$r = Request::current();
						$r->redirect($this->controller.'/karyawan');
					  } else {
						$this->message->store_message('d_fails');
					  }
				  break;

		
		case "password_update":
				  // Save Password
				  $p['hpassword'] 				= sha1($_POST['new_password']);
				  
				  $query = DB::update('users')
					  ->where('id', '=', $id)
					  ->limit(1)
					  ->set($p);
				  $result = $query->execute();
					  if ($result == 1) {
							$this->message->store_message('u_success');
							$r = Request::current();
							$r->redirect($this->controller.'/karyawan?id='.$id);
					  } else {
							$this->message->store_message('u_fails');
					  }
				  break;
		
		
		}

		/*pencarian start*/
		$key   = Arr::get($_REQUEST,'key');
		$value = Arr::get($_REQUEST,'value');
		/*pencarian end*/

		$all_items = DB::select()
			->from('users')
			->where('userroles_key','=','200');

		/*pencarian start*/
		if(!empty($key))
		{
			if(!empty($value)) {
				$all_items = $all_items->where($key, 'LIKE', '%'.$value.'%');
			}
		}
		/*pencarian end*/

		$all_items = $all_items->execute();
		$count = $all_items->count();
		
		$userlists = DB::select()
			->from('users')
			->limit($rpp)
			->offset($offset)
			->where('userroles_key','=','200')
			->where('id','!=','1');

		/*pencarian start*/
		if(!empty($key))
		{
			if(!empty($value)) {
				$userlists = $userlists->where($key, 'LIKE', '%'.$value.'%');
			}
		}
		/*pencarian end*/

		$userlists = $userlists->order_by('id', 'ASC');
		$userlists = $userlists->execute();
		
		$userupdate = DB::select()
			->from('users')
			->where('id', '=', $id)
			->limit(1)
			->execute();
		
		$pagination = Helper_myFunctions::factory()->paginations($count,$rpp);
		
		$content['title']		= 'User Administrator';
		$content['content']	= View::factory('inc/karyawan')
			->set('userlists',$userlists)
			->set('userupdate',$userupdate[0])
			->set('pagination',$pagination)
			->render();
		
		return $content;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}